import requests
import time
from lxml import etree
import threading
class DouBanSpider:
    def __init__(self):
        self.base_url = "https://movie.douban.com/top250?start={}&filter="
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
    def get_html(self, url):
        """向 url 发送请求，获取相应字符串"""
        resp = requests.get(url=url, headers=self.headers)
        return resp.content.decode()  # 响应的内容从字节格式解码为字符串格式

    def parse_page(self, url):
        """解析逻辑，从列表中拿到 url，然后提取里面的数据"""
        html = self.get_html(url)
        p = etree.HTML(html)
        # 获取到一页的电影数据
        node_list = p.xpath("//div[@class='info']")
        for move in node_list:
            # 电影名称
            title = move.xpath('.//a/span/text()')[0]
            # 评分
            score = move.xpath('.//div[@class="bd"]//span[@class="rating_num"]/text()')[0]
            print("电影名:%s, 评分:%s" % (title, score))

    def run(self):
        """多线程去抓取数据"""
        threads = []
        for i in range(10):
            url = self.base_url.format(i * 25)
            t = threading.Thread(target=self.parse_page, args=(url,))
            threads.append(t)
            t.start()

        for t in threads:
            t.join()

if __name__ == "__main__":
    start = time.time()  # 记录程序开始执行时间
    spider = DouBanSpider()
    spider.run()
    end = time.time()
    print("一共耗时:%.2f 秒" % (end - start))